package com.sc.nft.admin.controller;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.api.R;
import com.sc.nft.admin.request.*;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.entity.vo.CoefficientVO;
import com.sc.nft.entity.vo.ecology.*;
import com.sc.nft.entity.vo.merchantPledgeTask.AdminEcologyIncomeConfListVO;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.AdminEcologyCastConfService;
import com.sc.nft.service.AdminEcologyIncomeConfService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

import java.sql.Timestamp;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/ecology/income")
@Api(tags = "分润配置")
public class AdminEcologyIncomeConfController {


    private final AdminEcologyIncomeConfService adminEcologyIncomeConfService;
    private final Redisson redisson;


    @PostMapping(value = "/addConf", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新增收益配置", notes = "新增收益配置", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addConf(@Valid @RequestBody ADDEcologyIncomeConfRequest request) {
        RLock lock = redisson.getLock(RedisKeyEnum.ADMIN_ECOLOGY_INCOME_CONF_LOCK.getKey() + request.getConfId());
        try {
            boolean res = lock.tryLock();
            if (res) {
                adminEcologyIncomeConfService.addConf(request.getConfId(), request.getPlatformNotDivideIntoNum(), request.getDaoNotDivideIntoNum(), request.getType(),
                        request.getResonanceStatus(), request.getResonanceServiceCenterDaoId(), request.getConfDetailsList(), request.getResonanceServiceCenterCoefficientConfDTOList());
            } else {
                throw new GlobalRunTimeException("当前有操作人员进行操作，稍后重试");
            }
        } catch (Exception e) {
            throw new GlobalRunTimeException(e.getMessage());
        } finally {
            lock.unlock();
        }
        return Result.success();
    }

    @PostMapping(value = "/coefficient", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取城市服务中心对应的分润系数", notes = "获取城市服务中心对应的分润系数", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<CoefficientVO> getCoefficient(@Valid @RequestBody ADDEcologyIncomeConfRequest request) {
        adminEcologyIncomeConfService.getCoefficient(request.getResonanceServiceCenterDaoId(), request.getConfId(), null, request.getResonanceServiceCenterCoefficientConfDTOList());
        return Result.success();
    }

    @PostMapping(value = "/getConfList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取收益配置列表", notes = "获取收益配置列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AdminEcologyIncomeConfListVO> getConfList(@Valid @RequestBody AdminEcologyIncomeRequest request) {
        return PageResult.success(adminEcologyIncomeConfService.getConfList(request.getCollectionName(), request.getDaoName(), request.getPageNo(), request.getPageSize()));
    }

    @PostMapping(value = "/getConfById", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取收益配置详情", notes = "获取收益配置", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<AdminEcologyHatchCollecionShareProfitConfDetailsVO> getConfById(@Valid @RequestBody IdRequest request) {
        return Result.success(adminEcologyIncomeConfService.getConfById(request.getId()));
    }

    @PostMapping(value = "/updateConf", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改收益配置", notes = "获取收益配置", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateConf(@Valid @RequestBody AdminEcologyIncomeConfUpdateVO req) {
        RLock lock = redisson.getLock(RedisKeyEnum.ADMIN_ECOLOGY_INCOME_CONF_LOCK.getKey() + req.getConfId());
        try {
            boolean res = lock.tryLock();
            if (res) {
                adminEcologyIncomeConfService.updateConf(req);
            } else {
                throw new GlobalRunTimeException("当前有操作人员进行操作，稍后重试");
            }
        } catch (Exception e) {
            throw new GlobalRunTimeException(e.getMessage());
        } finally {
            lock.unlock();
        }
        return Result.success();
    }

    @PostMapping(value = "/updateSeparateAccountType", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改双方分账配置", notes = "修改双方分账配置", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateConf(@Valid @RequestBody UpdateSeparateAccountTypeRequest request) {
        adminEcologyIncomeConfService.updateSeparateAccountType(request.getConfId(), request.getSeparateAccountType());
        return Result.success();
    }


    @PostMapping(value = "/divideIntoPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "分成记录", notes = "分成记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AdminEcologyHatchDivideIntoLogVO> divideIntoPage(@Valid @RequestBody IdPageRequest request) {
        return PageResult.success(adminEcologyIncomeConfService.divideIntoPage(request.getId(), request.getPageNo(), request.getPageSize()));
    }





    @PostMapping(value = "/addConfNew", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新增收益配置(新)", notes = "新增收益配置(新)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addConfNew(@Valid @RequestBody ADDEcologyIncomeConfNewRequest request) {
        RLock lock = redisson.getLock(RedisKeyEnum.ADMIN_ECOLOGY_INCOME_CONF_LOCK.getKey() + request.getConfId());
        try {
            boolean res = lock.tryLock();
            if (res) {
                adminEcologyIncomeConfService.addConfNew(request.getConfId(), request.getSingleList(),request.getServiceCenterList());
            } else {
                throw new GlobalRunTimeException("当前有操作人员进行操作，稍后重试");
            }
        } catch (Exception e) {
            throw new GlobalRunTimeException(e.getMessage());
        } finally {
            lock.unlock();
        }
        return Result.success();
    }


    @PostMapping(value = "/getNewByConfId", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "收益配置详情(新)", notes = "收益配置详情(新)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<ADDEcologyIncomeConfNewDetailsVO> getNewByConfId(@Valid @RequestBody IdRequest request) {
        return Result.success(adminEcologyIncomeConfService.getNewByConfId(request.getId()));
    }




    @PostMapping(value = "/updateConfNew", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "更新收益配置(新)", notes = "更新收益配置(新)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateConfNew(@Valid @RequestBody ADDEcologyIncomeConfNewRequest request) {
        RLock lock = redisson.getLock(RedisKeyEnum.ADMIN_ECOLOGY_INCOME_CONF_LOCK.getKey() + request.getConfId());
        try {
            boolean res = lock.tryLock();
            if (res) {
                adminEcologyIncomeConfService.updateConfNew(request.getConfId(), request.getSingleList(),request.getServiceCenterList());
            } else {
                throw new GlobalRunTimeException("当前有操作人员进行操作，稍后重试");
            }
        } catch (Exception e) {
            throw new GlobalRunTimeException(e.getMessage());
        } finally {
            lock.unlock();
        }
        return Result.success();
    }



    @PostMapping(value = "/getNewConfList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取收益配置列表(新)", notes = "获取收益配置列表(新)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AdminEcologyIncomeConfListVO> getNewConfList(@Valid @RequestBody AdminEcologyIncomeRequest request) {
        return PageResult.success(adminEcologyIncomeConfService.getNewConfList(request.getCollectionName(), request.getDaoName(), request.getPageNo(), request.getPageSize()));
    }


}
